<?php

namespace App\Admin\Controllers\Plant;

use App\Models\AdminUser;
use App\Models\Department;
use App\PlantSale;
use App\Rules\MoneyRule;
use App\Services\PlantSaleService;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;


class PlantSaleController extends AdminController
{
    protected $title = '植物新品种已转化';
    public $repository;
    protected $service;

    public function __construct(PlantSaleService $service)
    {
        $this->service = $service;
    }

    protected function grid(): Grid
    {
        $grid = new Grid(new PlantSale());
        $this->service->gridColumn($grid, 1);
        $grid->disableCreateButton(false);
        $grid->disableActions(false);
        $grid->batchActions(function(Grid\Tools\BatchActions $actions){
            $actions->disableDelete(false);
        });
        return $grid;
    }

    protected function form()
    {
        $form = new Form(new PlantSale());
        $form->text('sn', '审定编号')->required();
        $form->text('name', '名称')->required();
        $form->text('buyer', __('受让方'))->required();
        $form->text('sale_amount', __('转化金额（万）'))->rules(['required',new MoneyRule()]);
        $form->select('sale_type', '转化类型')->options(select_sale_type_options())->required();
        $form->date('sale_date', '转化日期');
        $form->select('department_id', '负责单位')->options(Department::departmentOptions());
        $form->select('user_id', '负责人')->options(AdminUser::userOptions());
        $form->text('contact_person', '联系人');
        $form->text('contact_mobile', '联系电话');
        $form->saving(function (Form $form) {
            $form->department_id = $form->department_id ?: 0;
            $form->user_id = $form->user_id ?: 0;
        });
        return $form;
    }







}
